<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes"/>
<title>C7讲解-24航C | pansis.io</title>
<link rel="shortcut icon" href="https://github.pansis.site/favicon.ico">
<link href="https://github.pansis.site/styles/main.css" rel="stylesheet">
<link href="//at.alicdn.com/t/c/font_1678829_b85ccgkdqkr.css" rel="stylesheet">
<link href="//cdnjs.cloudflare.com/ajax/libs/KaTeX/0.10.0/katex.min.css" rel="stylesheet">
<link rel="alternate" type="application/rss+xml" title="pansis.io » Feed" href="https://github.pansis.site/atom.xml">
        <meta name="description" content="A C909

scanf(&quot;%s&quot;,a)与gets(a)
printf(&quot;%s&quot;,a)与puts(a)

代码


#include &lt;stdio.h&gt;
#include &lt;std..." />
        <meta name="keywords" content="24航C" />
        <!-- OG -->
        <meta property="og:locale" content="zh_CN">
        <meta property="og:title" content="C7讲解-24航C" />
        <meta property="og:type" content="article" />
        <meta property="og:description" content="A C909

scanf(&amp;quot;%s&amp;quot;,a)与gets(a)
printf(&amp;quot;%s&amp;quot;,a)与puts(a)

代码


#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;std...">
        <meta property="og:url" content="https://github.pansis.site/post/C7讲解-24航C/" />
        <meta property="og:site_name" content="pansis.io">
        <meta property="og:updated_time" content="2024-11-18">
        <meta property="og:image" content="" />
        <meta property="og:image:secure_url" content="">
        <meta property="og:image:alt" content="C7讲解-24航C">
        <!-- Twitter (post.ejs) -->
        <meta name="twitter:card" content="summary_large_image">
        <meta name="twitter:title" content="C7讲解-24航C">
        <meta name="twitter:description" content="A C909

scanf(&amp;quot;%s&amp;quot;,a)与gets(a)
printf(&amp;quot;%s&amp;quot;,a)与puts(a)

代码


#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;std...">
        <!-- <meta name="twitter:site" content="@WBoy0609">
        <meta name="twitter:creator" content="@WBoy0609"> -->
        <meta name="twitter:image" content="">
</head>

<body>
    <div class="main animated">
        <div class="header animated fadeInDown">
    <div class="site_title_container">
        <div class="site_title">
            <a href="https://github.pansis.site">pansis.io</a>
        </div>
    </div>
    <div class="my_socials">
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
        <a href="https://github.pansis.site/atom.xml" title="rss" target="_blank"><i class="iconfont icon-rss"></i></a>
    </div>
</div>

    <div class="header_menu">
        
            
                <a href="/" class="menu">首页</a>
            
        
            
                <a href="/tag/GWAaV2nvk/" class="menu">程序设计课程</a>
            
        
            
                <a href="/tag/24hangc" class="menu">比赛</a>
            
        
            
                <a href="/tag/L7r9STb75/" class="menu">Python教程</a>
            
        
            
                <a href="/tags" class="menu">分类</a>
            
        
        <div class="gridea-search-div">
            <form id="gridea-search-form" action="https://github.pansis.site/search/">
                <input class="gridea-search-input" autocomplete="off" spellcheck="false" name="q"/>
            </form>
        </div>
    </div>

            <div class="autopagerize_page_element">
                <div class="content">
                    <div class="post_page">
                        <div class="post animated fadeInDown">
                            <div class="post_title post_detail_title">
                                <h2>
                                    C7讲解-24航C
                                </h2>
                                <span class="article-info">
                                    2024-11-18, 1362 words, 8 min read
                                </span>
                            </div>
                            <div class="post_content markdown">
                                <p class="md_block">
                                    <span class="md_line md_line_start md_line_end">
                                        <h1 id="a-c909"><code>A</code> C909</h1>
<ul>
<li>scanf(&quot;%s&quot;,a)与gets(a)</li>
<li>printf(&quot;%s&quot;,a)与puts(a)</li>
</ul>
<h4 id="代码">代码</h4>
<pre><code class="language-c">

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;math.h&gt;
char s[20][200]={
        &quot;The country's first home-developed regional passenger jet, the ARJ21,\n&quot;,
        &quot;has added a new branding name C909,\n&quot;,
        &quot;Shanghai-based Commercial Aircraft Corp of China said on\n&quot;,
        &quot;Tuesday during the 15th China International Aviation\n&quot;,
        &quot;and Aerospace Exhibition,\n&quot;,
        &quot;also known as the Zhuhai Airshow, in Zhuhai, Guangdong province.\n&quot;,
        &quot;A total of 150 C909 jets have been delivered to different carriers\n&quot;,
        &quot;since its commercial launch in 2016 and safely carried over 17 million passenger trips.\n&quot;,
        &quot;The aircraft manufacturer COMAC said\n&quot;,
        &quot;it has continued to enhance the performance,\n&quot;,
        &quot;crew operation experience,\n&quot;,
        &quot;and cabin comfort of the C909 aircraft.\n&quot;};
int main()
{
    int l,r;
    scanf(&quot;%d%d&quot;,&amp;l,&amp;r);
    for (int i = l; i &lt;=r ; ++i) {
        printf(&quot;%s&quot;,s[i-1]);
    }
}
</code></pre>
<h1 id="b-小宇的字符查找"><code>B</code> 小宇的字符查找</h1>
<h4 id="涉及数字与字符的混合输入">涉及数字与字符的混合输入</h4>
<p>清楚理解scanf(&quot;%s&quot;)和gets()的区别（读入前、终止条件、读入后）</p>
<p>清楚理解读取数字的scanf(&quot;%d&quot;) %lld,%lf在读入前、终止条件、读入后的行为</p>
<ul>
<li>scanf(&quot;%s&quot;)和gets()</li>
</ul>
<table>
<thead>
<tr>
<th></th>
<th>读入前</th>
<th>中止条件</th>
<th>读入后</th>
</tr>
</thead>
<tbody>
<tr>
<td>scanf(&quot;%s&quot;)</td>
<td>会忽略\n和空格</td>
<td>遇到空格或\n</td>
<td>空格或\n会留在缓冲区内</td>
</tr>
<tr>
<td>gets()</td>
<td>不会忽略\n</td>
<td>遇到\n</td>
<td>把\n读入并转换为0作为字符串结束符</td>
</tr>
</tbody>
</table>
<ul>
<li>scanf(&quot;%c&quot;)与getchar()</li>
</ul>
<p>在字符读入上完全等价，只是用法和返回值不同。</p>
<pre><code>scanf(&quot;%c&quot;,&amp;a);
a=getchar();
   123a
1234
</code></pre>
<ul>
<li>
<p>%d等数字读入</p>
<p>%d、%lld、%lf等数字读入</p>
<table>
<thead>
<tr>
<th></th>
<th>读入前</th>
<th>中止条件</th>
<th>读入后</th>
</tr>
</thead>
<tbody>
<tr>
<td>%d、%lld、%lf</td>
<td>忽略所有空白符</td>
<td>遇到非数字字符</td>
<td>不作处理</td>
</tr>
</tbody>
</table>
<p>如果在读入前遇到了非数字字符，直接读入失败，不进行读入和赋值</p>
</li>
<li>
<p>何时需要使用getchar()</p>
<ul>
<li>当先用%d读入一个数字，然后接着用gets()或getchar()、%c去读下一行的字符或字符串。</li>
<li>此时%d只会读数字，而\n会留下来。</li>
<li>因此需要getchar()去把\n吃掉，方便后续使用gets()或getchar()、%c去读下一行的字符或字符串。</li>
</ul>
<p>实例</p>
<pre><code> 123 123 123
abcd
cdef
</code></pre>
<pre><code class="language-c">#include &lt;stdio.h&gt;
int main()
{
    int a,b,c,d;
    double e,f,g;
    char str1[100];
    char str2[100];
    char str3[100];
    scanf(&quot;%d%d%d&quot;,&amp;a,&amp;b,&amp;c);
    //scanf(&quot;%s&quot;,str1);
    //gets(str2);
    //gets(str3);
    return 0;
}
</code></pre>
</li>
</ul>
<h4 id="strchrs-c-strrchrs-c">strchr(s, c)、strrchr(s, c)</h4>
<pre><code class="language-c"> strchr()和strrchr()
1.函数声明：
  char *strchr(const char *s,int c);
  char *strrchr(const char *s,int c);
2.功能：若字符串s中存在字符c，strchr()和strrchr()分别返回字符c在s中第一次和最后一次出现的位置的指针。
      若字符串s中没有字符c，则返回NULL
</code></pre>
<h4 id="代码-2">代码</h4>
<pre><code class="language-c">#include &lt;stdio.h&gt;
#include &lt;string.h&gt;


int main() {
    char s[1005];
    gets(s);
    char c;
    while (scanf(&quot;%c&quot;, &amp;c) != EOF) {
        getchar();
        if (strchr(s, c) != NULL) {
            int first = strchr(s, c) - s;
            int last = strrchr(s, c) - s;
            printf(&quot;%d %d\n&quot;, first, last);
        } else {
            printf(&quot;-1\n&quot;);
        }
    }
}
</code></pre>
<pre><code class="language-c">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;math.h&gt;
char s[1010];
int wei[1010];


int main(){
    gets(s);
    int len=strlen(s);
    char a;
    int i,j;
    int flag=0;
    while(scanf(&quot;%c&quot;,&amp;a)!=EOF)
    {
        flag=0;

        for(i=0;i&lt;len;i++)
        {
            if(a==s[i])
            {
                flag=1;
                printf(&quot;%d &quot;,i);
                break;
            }
        }
        for(j=len-1;j&gt;=0;j--)
        {
            if(a==s[j])
            {
                flag=1;
                printf(&quot;%d\n&quot;,j);
                break;
            }
        }
        if(flag==0)
        {
            printf(&quot;-1\n&quot;);

        }
        getchar();
    }
    return 0;


}
</code></pre>
<h1 id="c-翻转加密"><code>C</code> 翻转加密</h1>
<pre><code class="language-c">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;math.h&gt;


int main(){
	char s[105];
    gets(s);
    int a,b;
    scanf(&quot;%d%d&quot;,&amp;a,&amp;b);
    while (a&lt;b)//不能写a!=b
    {
        char temp=s[a];
        s[a]=s[b];
        s[b]=temp;
        a++;b--;
    }
    puts(s);

}
</code></pre>
<h1 id="d-循环子序列"><code>D</code> 循环子序列</h1>
<h4 id="代码-3">代码</h4>
<pre><code class="language-c">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;math.h&gt;
char s0[100005];
char s1[100005];
char s[200005];
int flag[200005];
int main()
{
    gets(s0);
    gets(s1);
    int s0len=strlen(s0);
    int s1len=strlen(s1);
    for (int i = 0; i &lt; s0len; ++i) {
        s[i]=s0[i];
    }
    for (int i = 0; i &lt; s0len; ++i) {
        s[i+s0len]=s0[i];
    }
    s[s0len*2]=0;
    char *p=s;
    while ((p=strstr(p,s1))!=NULL)
    {
        int start=p-s;
        for (int i = start; i &lt;s1len+start ; ++i) {
            flag[i]=1;
        }
        p+=s1len;
    }
    for (int i = 0; i &lt; s0len; ++i) {
        if (flag[i]==1||flag[i+s0len]==1)
            printf(&quot;%c&quot;,s0[i]);
        else
            printf(&quot;*&quot;);
    }



}
</code></pre>
<h1 id="e-帕林卓姆"><code>E</code> 帕林卓姆</h1>
<h4 id="样例1">样例1</h4>
<pre><code class="language-c">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;math.h&gt;


int main(){
    char s[1005];
    while (gets(s)!=NULL)
    {
        int len= strlen(s);
        int flag=0;
        for (int i = 0; i &lt; len; ++i) {
            if (s[i]!=s[len-1-i])
            {
                printf(&quot;NO\n&quot;);
                flag=1;
                break;
            }
        }
        if (flag==0)
            printf(&quot;YES\n&quot;);
    }


}
</code></pre>
<h4 id="样例2">样例2</h4>
<pre><code class="language-c">#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

int main()
{
    char s[1005];
    

    while(~scanf(&quot;%s&quot;, s))
    {
    	char res[1005]={0};
        
        int len = strlen(s);

        for(int i = 0; i &lt; len; i++)
        {
            res[i] = s[len-i-1];
        }


        if (strcmp(s, res))
            printf(&quot;NO\n&quot;);
        else
            printf(&quot;YES\n&quot;);
    }

   
    return 0;
}
</code></pre>
<h4 id="代码-4">代码</h4>
<pre><code class="language-c">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;math.h&gt;
int find(char s[])
{
    char *p=s-1;
    int num=0;
    while (p!=NULL)
    {
        p++;num++;
        p=strstr(p,&quot;CVBB&quot;);
    }
    num--;
    return num;

}

int main(){
    int q;
    char s[1005];
    gets(s);
    scanf(&quot;%d&quot;,&amp;q);
    for (int i = 0; i &lt; q; ++i) {
        int x;char c;
        scanf(&quot;%d %c&quot;,&amp;x,&amp;c);
        s[x-1]=c;
        printf(&quot;%d\n&quot;,find(s));
    }

}
</code></pre>
<p>字符串输出函数</p>
<ul>
<li>puts()</li>
<li>fputs()</li>
<li>printf()</li>
<li>sprintf() (字符串构造)</li>
</ul>
<p>字符串输入函数</p>
<ul>
<li>scanf()</li>
<li>gets()</li>
<li>fgets()</li>
</ul>
<p>字符串复制</p>
<ul>
<li>strcpy()</li>
<li>strncpy()</li>
</ul>
<p>字符串追加函数</p>
<ul>
<li>strcat()</li>
<li>strncat()</li>
</ul>
<p>字符串比较函数</p>
<ul>
<li>strcmp()</li>
<li>strncmp()</li>
</ul>
<p>字符串检查函数</p>
<ul>
<li>strlen()长度</li>
<li>strchr() strrchr() 查找字符位置</li>
<li>strstr() 查找子字符串位置</li>
</ul>
<p>字符串扫描函数</p>
<ul>
<li>sscanf()</li>
</ul>
<br />
                                            
                                </p>
                            </div>
                            <div class="post_footer">
                                
                                    <div class="meta">
                                        <div class="info"><span class="field tags"><i class="iconfont icon-tag-sm"></i>
                                                
                                                    <a href="https://github.pansis.site/tag/24hangc/" class="article-info">
                                                        24航C
                                                    </a>
                                                    
                                            </span>
                                        </div>
                                    </div>
                                    
                                        
                                            <div class="next-post" style="margin-top: 20px;">
                                                <div class="next">下一篇</div>
                                                <a href="https://github.pansis.site/post/C7 - Solution-24航C/">
                                                    <h3 class="post-title">
                                                        C7 - Solution-24航C
                                                    </h3>
                                                </a>
                                            </div>
                                            
                            </div>
                        </div>
                        
                            
                                <link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css">
<script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>
<div id="gitalk-container" style="padding-bottom: 20px;"></div>
<script>
    var pageId = (location.pathname).substring(1, 49) // Ensure uniqueness and length less than 50
    pageId = pageId.endsWith('/') ? pageId.slice(0, -1) : pageId // 以斜杠结尾则去除
    var gitalk = new Gitalk({
        clientID: '9d5eba33618472c44a07',
        clientSecret: '065a85ed04333ceebfc4f01d7ca1674175730339',
        repo: 'fzxl2003.github.io',
        owner: 'fzxl2003',
        admin: ['fzxl2003'],
        id: pageId,
        distractionFreeMode: false  // Facebook-like distraction free mode
    })
    gitalk.render('gitalk-container')
</script>
                                    
                                        
                                                    
                    </div>
                </div>
            </div>
    </div>
    <div class="footer">
    
    <div class="powered_by">
        <a href="https://codeberg.org/kytrun/gridea-theme-one" target="_blank">Theme One,</a>
        <a href="https://open.gridea.dev/" target="_blank">Powered by Gridea&#65281;</a>
    </div>
    
    
        <div class="footer_slogan">
            Powered by <a href="https://github.com/getgridea/gridea" target="_blank">Gridea</a>
        </div>
    
    <div id="back_to_top" class="back_to_top">
        <span>△</span>
    </div>
    
</div>

<script src="https://github.pansis.site/media/scripts/util.js"></script>
        <link rel="stylesheet" href="//unpkg.com/@highlightjs/cdn-assets@11.5.1/styles/default.min.css">
        <script src="//unpkg.com/@highlightjs/cdn-assets@11.5.1/highlight.min.js"></script>
        <script>hljs.highlightAll();</script>
</body>

</html>